<!--
 * @Author: jinjing
 * @Date: 2024-11-23
 * @Description: 基金净值。
 -->
<template>
  <div class="content">
    <el-select v-model="fundCode" placeholder="请选择基金代码" @change="handleChange">
      <el-option 
        v-for="item in fundCodeList" 
        :key="item.ticker"
        :label="item.name + `(${item.ticker})`"
        :value="item.ticker"
        ></el-option>
    </el-select>
    <el-table 
        :data="tableData.slice((page.currentPage-1) * page.pageSize, page.currentPage * page.pageSize)" 
        style="width: 100%; height: 500px;" 
        v-loading="loading"
        >
        <el-table-column prop="dt" label="日期"/>
        <el-table-column prop="nav" label="单位净值"/>
    </el-table>
    <div class="page-content">
        <el-pagination
            :current-page="page.currentPage"
            :page-size="page.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            :disabled=false
            :background=false
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>
    
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { getFundData, getNavData } from '../duckdb';
import moment from 'moment'

const fundCode = ref('')
const fundCodeList = ref([])
const loading = ref(false)
const tableData = ref([])
const page = ref({
    currentPage: 1,
    pageSize: 10,
    total: 0,
})

const getListData = async () => {
    let ans = await getFundData("select ticker,name from fund_desc")
    fundCodeList.value = ans
    return fundCodeList
}

await getListData()

const handleChange = async () => {
    loading.value = true
    let ans = await getNavData(`select dt,nav from fund_nav where ticker='${fundCode.value}'`)
    for (let row of ans) {
        let date = new Date(row['dt'])
        row['dt'] = moment(date).format('YYYY-MM-DD HH:mm:ss')
        tableData.value.push(row)
    }
    page.value.total = tableData.value.length
    loading.value = false
}

const handleCurrentChange = async (num) => {
    page.value.currentPage = num
}

const handleSizeChange = async (num) => {
    page.value.pageSize = num
}

</script>

<style>

</style>